from django.core.paginator import Paginator
from django.shortcuts import render, redirect
from django.views import View
from django.http import HttpResponse
from django import forms
from django.contrib.auth import login, logout, authenticate

from .models import User
from .models import Train
from .models import Book
import random
# Create your views here.
import re



###############################################
###############################################
remail = r'.^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(.[a-zA-Z0-9_-]+)+$'


###############################################
###############################################

def index_view(request):
    # logout(request)
    # html data
    orderuser = request.GET.get('orderuser')
    # orderuser = 'Wyatt'
    ticketnumber = request.POST.get('ticket_number')
    train = 1024
    seat = request.POST.get('seat')
    print(orderuser)
    print(ticketnumber)
    print(train)
    print(seat)

    # database data
    db = User.objects.all()
    db = db[0:100]

    tr = Train.objects.all()
    tr = tr[0:100]
    a = random.randint(0, 8)
    b = random.randint(8, 15)
    tr_info = tr[a:b]

    po_list = []

    for i in tr_info:
        po_list.append(i)

    # paginate
    page_num = request.GET.get('page', 1)
    all_data = po_list

    # init paginator
    paginator = Paginator(all_data, 5)

    # page object
    c_page = paginator.page(int(page_num))

    # for i in db:
    #     # print(i.User)
    #     userlist.append(i.User)
    #
    # for user in userlist:
    #     print(user)
    return render(request, 'index_wel.html', locals())


def delete_view(request):

    db = Book.objects.filter(User=request.user)
    for d in db:
        d.delete()

    return render(request, 'deleteResult.html', locals())


def orderSuccess(request):
    # html data
    orderuser = request.POST.get('orderuser')
    orderUserObject = User.objects.get(username=orderuser)
    # orderuser = 'Wyatt'
    ticketnumber = request.POST.get('ticket_number')
    # train = 1024
    train = request.POST.get('train')
    trainObject = Train.objects.get(ID=train)
    seat = request.POST.get('seat')
    print(orderuser)
    print(ticketnumber)
    print(train)
    print(seat)

    # database data
    # db = Book.objects.all()
    #
    # userlist = []
    #
    # for i in db:
    #     # print(i.User)
    #     userlist.append(i.User)

    # for user in userlist:
    #     print(user)
    t = Book.objects.create(User=request.user, TicketNumber=ticketnumber, Train=trainObject, Seat=seat)
    t.save()

    return render(request, 'orderSuccess.html', locals())


class LoginView(View):
    class LoginForm(forms.Form):
        username = forms.CharField(label='username', max_length=100, required=True)
        password = forms.CharField(label='password', max_length=100, required=True)

    def get(self, request, *args, **kwargs):
        return render(request, 'index_login.html')

    def post(self, request, *args, **kwargs):
        login_form = self.LoginForm(request.POST)
        if not login_form.is_valid():
            return HttpResponse('login_form not valid')
        user = authenticate(request,
            username=login_form.cleaned_data['username'],
            password=login_form.cleaned_data['password'],
        )
        if user is None:
            print(login_form.cleaned_data)
            return redirect('/customer/login')
        login(request, user)
        if user.is_superuser:
            return redirect('/admin/')
        return redirect('/customer/index')


def logout_view(request):
    logout(request)
    return redirect('/')


def main_view(request):

    # return render(request, 'testSearch.html', locals())

    return render(request, 'index.html')


def search_result(request):
    search = request.GET.get('search')
    print(search)

    db = Train.objects.all()
    print(db)

    po_list = []

    for i in db:
        if search in i.To:
            po_list.append(i)

    # paginate
    page_num = request.GET.get('page', 1)
    all_data = po_list

    # init paginator
    paginator = Paginator(all_data, 50)

    # page object
    c_page = paginator.page(int(page_num))
    return render(request, 'searchResult.html', locals())


class RegisterView(View):
    class RegisterForm(forms.Form):
        username = forms.CharField(label='username', max_length=100, required=True)
        phone = forms.IntegerField(label='phone', required=True)
        password = forms.CharField(label='password', max_length=100, required=True)

    def get(self, request, *args, **kwargs):
        return HttpResponse('hello')

    def post(self, request, *args, **kwargs):
        register_form = self.RegisterForm(request.POST)
        if not register_form.is_valid():
            return HttpResponse('form not variable')

        user = User.objects.create_user(
            username = register_form.cleaned_data['username'],
            password = register_form.cleaned_data['password'],
            phone = register_form.cleaned_data['phone'],
            )
        user.save()
        return HttpResponse('User register successfully')


def horizontal_view(request):
    return render(request, 'index2.html')


def RTL_view(request):
    return render(request, 'dashboard-rtl.html')


def ecommerce_view(request):
    return render(request, 'ec-dashboard.html')


def blog_view(request):
    return render(request, 'blog-dashboard.html')


def appindex_view(request):
    return render(request, 'mail-inbox.html')


def chat_list(request):
    return render(request, 'chat.html')


def calendar_view(request):
    return render(request, 'events.html')


def file_view(request):
    return render(request, 'file-dashboard.html')


def connect_view(request):
    return render(request, 'contact.html')


def ecproduct_view(request):
    return render(request, 'ec-product.html')


def ecproductlist_view(request):
    return render(request, 'ec-product-List.html')


def ecproductdetail_view(request):
    return render(request, 'ec-product-detail.html')


def tables_view(request):
    return render(request, 'tables.html')


def tables_view(request):
    return render(request, 'tables.html')


def tables_view(request):
    return render(request, 'tables.html')


def tables_view(request):
    return render(request, 'tables.html')


def tables_view(request):
    return render(request, 'tables.html')
